//
//  main.c
//  5 chapter
//
//  Created by edz on 2020/6/29.
//  Copyright © 2020 edz. All rights reserved.
//

#include <stdio.h>

#if 0
// 练习5-1
int main(void)
{
    int i;
    int v[5];

    for (i = 0; i < 5; i++)
        v[i] = i ;

    for (i = 0; i < 5; i++)
        printf("v[%d] = %d\n", i, v[i]);

    return 0;
}
#endif


#if 0
// 练习5-2
int main(void)
{
    int i;
    int v[5];

    for (i = 0; i < 5; i++)
        v[i] = 5-i;

    for (i = 0; i < 5; i++)
        printf("v[%d] = %d\n", i, v[i]);

    return 0;
}
#endif


#if 0
// 练习5-3
int main(void)
{
    int i;
    int v[5] = {5, 4, 3, 2, 1};

    for (i = 0; i < 5; i++)
        printf("v[%d] = %d\n", i, v[i]);

    return 0;
}
#endif


#if 0
// 练习5-4
int main(void)
{
    int i;
    int a[5] = {17, 23, 36};
    int b[5];

    for (i = 0; i < 5; i++)
        b[4 - i] = a[i];

    puts("  a   b");
    puts("---------");
    for (i = 0; i < 5; i++)
        printf("%4d%4d\n", a[i], b[i]);

    return 0;
}
#endif


#if 0
// 练习5-5
#define NUMBER 7
int main(void)
{
    int i;
    int x[NUMBER];

    for (i = 0; i < NUMBER; i++)
    {
        printf("x[%d]:", i);
        scanf("%d", &x[i]);
    }

    for (i = 0; i < NUMBER/2; i++)
    {
        int temp = x[i];
        x[i] = x[NUMBER - 1 - i];
        x[NUMBER - 1 - i] = temp;
    }

    puts("倒序排列了。");
    for (i = 0; i < NUMBER; i++)
        printf("x[%d] = %d\n", i, x[i]);

    return 0;
}
#endif


#if 0
// 练习5-6(a,b都是1)
int main(){
    double a;
    int b;
    a = b = 1.5;
    printf("a = %lf ; b = %d\n",a,b);
    return 0;
}
#endif


#if 0
// 练习5-7
#define NUMBER 100
int main(){
    int n,a[NUMBER];
    printf("数据个数:");
    scanf("%d",&n);
    
    for (int i = 0; i < n; i++) {
        printf("a[%d] = ",i);
        scanf("%d",&a[i]);
    }
    printf("{");
    for (int i = 0; i < n; i++) {
        printf("%d, ",a[i]);
    }
    printf("}\n");
    return 0;
}
#endif


#if 0
// 练习5-8
#define NUMBER 80    /*人数上限*/

int main(void)
{
    int i, j;
    int num;              /*实际的人数*/
    int fenshu[NUMBER];    /*学生的分数*/
    int fenbu[11] = {0};  /*分布图*/

    printf("请输入学生人数：");
    do {
        scanf("%d", &num);
        if (num < 1 || num > NUMBER)
            printf("请输入1-%d的数：", NUMBER);
    } while (num < 1 || num > NUMBER);
    
    printf("请输入%d人的分数。\n", num);

    for (i = 0; i < num; i++)
    {
        printf("%2d号:", i + 1);
        do {
            scanf("%d", &fenshu[i]);
            if (fenshu[i] < 0 || fenshu[i] > 100)
                printf("请输入1-100的数：");
        } while (fenshu[i] < 0 || fenshu[i] > 100);
        fenbu[fenshu[i] / 10]++;
    }

    puts("\n---分布图---");

    for (i = 0 ; i <= 9; i++)
    {
        printf("%2d-%2d:", i * 10, i * 10 + 9);
        for (j = 0; j < fenbu[i]; j++)
            putchar('*');
        putchar('\n');
    }
    printf("  100:");
    for (j = 0; j < fenbu[10]; j++)
        putchar('*');
    putchar('\n');

    return 0;
}
#endif



#if 0
// 练习5-9
#define NUMBER 80


int main(void)
{
    int i, j;
    int a=0,b=0;
    int num;              /*实际的人数*/
    int fenshu[NUMBER];    /*学生的分数*/
    int fenbu[11] = {0};  /*分布图*/
    
    printf("请输入学生人数：");
    do {
        scanf("%d", &num);
        if (num < 1 || num > NUMBER)
            printf("请输入1-%d的数：", NUMBER);
    } while (num < 1 || num > NUMBER);
    
    printf("请输入%d人的分数。\n", num);
    
    for (i = 0; i < num; i++)
    {
        printf("%2d号:", i + 1);
        do {
            scanf("%d", &fenshu[i]);
            if (fenshu[i] < 0 || fenshu[i] > 100)
                printf("请输入1-100的数：");
        } while (fenshu[i] < 0 || fenshu[i] > 100);
        fenbu[fenshu[i] / 10]++;
    }
    
    for (i = 0; i <= 10; i++) //判断星星有几行（a=b行）
    {
        if (fenbu[i] > a)
            a=b= fenbu[i];
    }
    for (i = 0; i < a; i++)
    {
        for (j = 0; j <= 10; j++)
        {
            if (fenbu[j] -b>=0) //判断每列有几个星星，只有每列星星数>=b时，才会输出
                printf(" *  ");
            else
                printf("    ");
        }
        b--;
        putchar('\n');
    }
    for (i = 0; i <= 10; i++)
        printf("----");
    putchar('\n');
    for (i = 0; i <= 10; i++)
        printf(" %d ", i * 10);
    printf("\n");
    return 0;
}
#endif


#if 0
// 练习5-10
#define a 3
#define b 4
 
int main(void)
{
    int la[a][b], lb[b][a], i, j;
    
    printf("请输入一个3行4列的矩阵。\n");
    
    for(i = 0; i < a; i++){
        printf("第%d行：",i+1);
        for(j = 0; j < b; j++){
            scanf("%d",&la[i][j]);
        }
    }
    
    printf("请输入一个4行3列的矩阵。\n");
    
    for(i = 0; i < b; i++){
        printf("第%d行：",i+1);
        for(j = 0; j < a; j++){
            scanf("%d",&lb[i][j]);
        }
    }
 
    for(i = 0; i < a; i++){
        for(j = 0; j < b; j++){
            printf("%d ",la[i][j]*lb[j][i]);
        }
        printf("\n");
    }
    
    return 0;
 } 
#endif



#if 0
// 练习5-11
#define stu 6
#define lesson 2
 
int main (void)
{
    int a, b;
    
    double la[stu][lesson], sum0, sum1 = 0, sum2 = 0;
    //sum0为各学生总成绩，sum1为语文总成绩， sum2为数学总成绩。
    
    for(a = 0; a < stu; a++){
        sum0 = 0;
        for(b = 0; b < lesson; b++){
            do{
                printf("请输入第%d名学生第%d门成绩。",a+1,b+1);
                scanf("%lf",&la[a][b]);
                if (b == 0)
                    sum1+=la[a][b];
                else
                    sum2+=la[a][b];
                if(la[a][b] > 100 || la[a][b] < 0){
                    printf("您输入的值过大，请重新输入。");
                }
            } while (la[a][b] > 100 || la[a][b] < 0);
            
            sum0+=la[a][b];
    }
            printf("第%d名学生的总分为%f,平均分为%f\n",a+1,sum0,sum0/lesson);
}
 
    printf("语文成绩的总分为%f,平均分为%f,数学成绩的总分为%f,平均分为%f\n",sum1,sum1/stu,sum2,sum2/stu);
     
    return 0;
 }
#endif

